<template>
	<view>
		<view class="u-page" :style="pageStyle">
			<view v-if="noticeConfig" :style="noticeConfig.style">
				<u-notice-bar :text="noticeConfig.text" @click="noticeBarClick(noticeConfig)"></u-notice-bar>
			</view>
            <view class="groupBoxStyle" :style="groupBoxStyle">
                <view class="btn" v-for="(item, index) in list" :key="index" :style="item.style" @click="btn0(item)"></view>
                <view class="group-menus" v-for="(g, gi) in groupMenus" :key="gi" :style="g.groupStyle" >
                	<view class="group-title" :style="g.titleStyle">{{g.title}}</view>
                	<view class="menus" :style="g.menuStyle">
                		<view class="btn" v-for="(item, index) in g.menus" :key="index" :style="item.style" v-show="!item.hide" @click="btn0(item)"></view>
                	</view>
                </view>
            </view>
			<registerLogin ref="loginPopup"></registerLogin>
		</view>
	</view>
</template>

<script>
	import getLocation from '@/utils/getLocation.js'
	import registerLogin from '@/components/register-login/registerLogin.vue'
	import sceneMixins from '@/utils/sceneMixins.js';
	import { mapState, mapMutations } from 'vuex';
	export default {
		mixins: [getLocation, sceneMixins],
		components: {registerLogin},
		data() {
			return {
				pageStyle: '',
                groupBoxStyle: '',
				list: [],
				groupMenus: [],
				noticeConfig: ''
			}
		},
		onLoad(options) {
			var vm = this;
			if (options.title) {
				uni.setNavigationBarTitle({
					title: options.title
				});
			}

			// debugger
			if (options.loadJSON) {
				vm.getAndLoad('申报的配置json', options.loadJSON, function (res) {
					vm.list = res.menus || [];
					vm.groupMenus = res.groupMenus || [];
					vm.pageStyle = res.pageStyle || '';
                    vm.groupBoxStyle = res.groupBoxStyle || '';
					vm.noticeConfig = res.noticeConfig;
				});
			}
			wx.showShareMenu({
				withShareTicket: true,
				menus: ["shareAppMessage", "shareTimeline"]
			});
			this.getUserLocation().then(res => {
				this.setPosition(res);
			});

		},
        onShareAppMessage(options) {
            let routes = getCurrentPages();
            let curParam = routes[routes.length - 1].options;
            return {
        		title: curParam.title,
        		path: 'pages/shenbao/home?title=' + curParam.title + '&loadJSON=' + curParam.loadJSON,
        		success(res) {
        			uni.showToast({
        				title: '分享成功'
        			})
        		},
        		fail(res) {
        			uni.showToast({
        				title: '分享失败',
        				icon: 'none'
        			})
        		}
        	}
        },
		computed: {
			...mapState(['hasLogin', 'globalConfig'])
		},
		methods: {
			...mapMutations(['setPosition']),
			noticeBarClick: function (config) {
				if (config.h5) {
					this.gotoH5Page(config.h5);
				} else if (config.text) {
					this.GM_showModel(noticeConfig.text);
				}
			},
			
			btn0: function (item) {
				var vm = this;
				// debugger
				if (item.needLogin && !vm.hasLogin) {
					this.$refs.loginPopup.openCommonPopup();
					return;
				}
				if (item.url) {
					var urlMore = '';
					if (item.urlQuery) {
						var splitCode = item.url.includes('?') ? '&' : '?';
						var query = encodeURIComponent(JSON.stringify(item.urlQuery));
						urlMore = splitCode + 'query=' + query;
					}
					uni.navigateTo({
						url: item.url + urlMore
					})
                    // debugger
					/*
					uni.requestSubscribeMessage({
						// tmplIds: ['yFSiGrkZN2VbE7bVZkufCSPwVJ0WeFUuJz9Mt9P-QNA'],
						tmplIds: vm.globalConfig.messageTepls,
						fail: function (res) {
							uni.showModal({
								title: '提示',
								content: '您可以打开消息提醒，以更快获取申报结果',
								success: function (res) {
									uni.openSetting({
										withSubscriptions: true,
										complete: () => {
											uni.navigateTo({
												url: item.url + urlMore
											})
										}
									});

								}
							});
						},
						success: function (res) {
							uni.navigateTo({
								url: item.url + urlMore
							})
						}
					});
					*/
				}  else if (item.event) {
					this.doEvent(item.event);
				} else if (item.h5) {
					vm.gotoH5Page(item.h5);
				} else {
					uni.$u.toast('该功能正在开发中，请明天再来');
				}
			},
			doEvent(event) {
				var vm = this;
				if (event == 'scan') {
                    vm.scanQrCode();
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-page {
		height: 100vh;
		overflow: auto;
	}

	.btn {
		margin: 30rpx 30rpx;
		width: 690rpx;
		height: 220rpx;
	}

	.btn1 {
		background: url(https://lyggn.05oa.com:8288/mobile/wxImages/1.png);
		backgroud-size: 100%;
	}
	
	.group-menus {
		background: #fff;
		margin: 30rpx;
		padding: 20rpx;
		border-radius: 20rpx;
		.group-title {
			height: 50rpx;
			line-height: 50rpx;
			font-size: 30rpx;
		}
		.menus {
			padding: 0 10rpx;
		}
		.btn {
			margin: unset;
			margin-bottom: 15rpx;
		}
	}
	
</style>